
#include <pic18.h>
#include "sys_clock.h"
#include "macros.h"
#include "dbg.h"
//volatile near bit TimeI_10ms;
volatile near bit TimeI_2ms;

//near bit Time_1ms;
/* near bit Time_2ms; */
/* near bit Time_10ms; */
/* near bit Time_100ms; */
/* near bit Time_500ms; */
/* near bit Time_1000ms; */
/* near bit Time_5000ms; */

#define   OSC_FREQ_16MHZ	 	0x70
#define   OSC_FREQ_8MHZ	 		0x60


void t0_init ( void )
{
	/* System Clock Init 18*/
	TMR0 = SYS_CLOCK_PERIOD;
	TMR0IF = 0; 
	TMR0IE = 1;
	T0CON = 0b10000100;			  /* divider: 32(bits0-2:100) */
}

/* 
bit 5-4 T1CKPS<1:0>: Timer1 Input Clock Prescale Select bits
        11 = 1:8 Prescale value
        10 = 1:4 Prescale value
        01 = 1:2 Prescale value
        00 = 1:1 Prescale value

 */
void t1_init ( void )
{
	TMR1 = 1;						  /*  */
	T1CON = 0b10100001;			  /* divider: 4(bits4-5:10); increment each 1us  */
}


/* 
 bit 6-4 IRCF<2:0>: Internal Oscillator Frequency Select bits
	111 = 16 MHz (HFINTOSC drives clock directly)
	110 = 8 MHz
	101 = 4 MHz
	100 = 2 MHz
	011 = 1 MHz(3)
	010 = 500 kHz
	001 = 250 kHz
	000 = 31 kHz (from either HFINTOSC/512 or LFINTOSC directly)(2)

 */

void sys_clock_init ( void )
{
	OSCCON = OSC_FREQ_16MHZ;
	t0_init();
	t1_init();
}

volatile near unsigned int sys_clock_cnt = 0;
unsigned int sclock;

void sys_clock( void )
{
	static unsigned int _sys_clock_cnt = 0;

	sclock = 0xffff;

	if ( _sys_clock_cnt != sys_clock_cnt )
	{
		sclock = _sys_clock_cnt = sys_clock_cnt;
	}
} 
